package view
{
	import map.data.RadCell;
	
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.geom.Point;
	
	public class RadCellView extends Sprite
	{
		public static const CLOSED:int = 0;
		public static const OPEN:int = 1;
		
		private var _id:String;
		public var point1:Point;
		public var point2:Point;
		public var point3:Point;
		public var point4:Point;
		private var _center:Point;
		private var _draw:Shape;
		private var _state:int = 0;
		private var _cell:RadCell;
		
		public function RadCellView(cell:RadCell)
		{
			super();
			_cell = cell;
			_draw = new Shape();
			addChild(_draw);
			this.point1 = cell.point1;
			this.point2 = cell.point2;
			this.point3 = cell.point3;
			this.point4 = cell.point4;
			this.graphics.beginFill(0xffffff,0.2);
			this.graphics.moveTo(point1.x,point1.y);
			this.graphics.lineTo(point2.x,point2.y);
			this.graphics.lineTo(point4.x,point4.y);
			this.graphics.lineTo(point3.x,point3.y);
			this.graphics.lineTo(point1.x,point1.y);
			this.graphics.endFill();
			
			var p1:Point = Point.interpolate(point1,point4,0.3);
			var p2:Point = Point.interpolate(point2,point3,0.3);
			var p3:Point = Point.interpolate(point3,point2,0.3);
			var p4:Point = Point.interpolate(point4,point1,0.3);
			_draw.graphics.lineStyle(2,0xffffff);
			_draw.graphics.moveTo(p1.x,p1.y);
			_draw.graphics.lineTo(p2.x,p2.y);
			_draw.graphics.lineTo(p4.x,p4.y);
			_draw.graphics.lineTo(p3.x,p3.y);
			_draw.graphics.lineTo(p1.x,p1.y);
			
			state = 0;
		}
		
		public function get center():Point
		{
			return _cell.center;
		}

		public function get state():int
		{
			return _state;
		}

		public function set state(value:int):void
		{
			_state = value;
			_draw.visible = _state>0;
		}
		
		public function clone():RadCellView
		{
			var result:RadCellView = new RadCellView(_cell);
			return result;
		}
		
		public function equal(cell:RadCellView):Boolean
		{
			return cell.point1.equals(point1) &&
				cell.point2.equals(point2) &&
				cell.point3.equals(point3) &&
				cell.point4.equals(point4);
		}
		
		public function getCloseInPoint(p:Point):Point
		{
			var result:Point = point1;
			if(Point.distance(point2,p)<=Point.distance(result,p))
			{
				result = point2;
			}
			if(Point.distance(point3,p)<=Point.distance(result,p))
			{
				result = point3;
			}
			if(Point.distance(point4,p)<=Point.distance(result,p))
			{
				result = point4;
			}
			return result;
		}
		
		public function get radCell():RadCell
		{
			return _cell;
		}
	}
}